//
//  TableViewController.m
//  quickLook
//
//  Created by J on 15/9/9.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "TableViewController.h"
#import "Header.h"
#import "QuickLookViewController.h"
#import "QuickLookCell.h"

#import "LandController.h"

#import "RightSearchController.h"

#import "MainPageLeftCell.h"
@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView* _tableView;
}
@property(nonatomic,strong)UIButton* landButton;

@end

@implementation TableViewController





//前往登录页面


- (void)gotoLand:(UIButton*)sender
{
    LandController* vc=[[LandController alloc]init];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
    
    
    
}

//前往导航栏搜索页面
- (void)pushCollectionView:(UIButton*)sender
{
    
    
    RightSearchController* vc=[[RightSearchController alloc]init];
    
    vc.hidesBottomBarWhenPushed=YES;
    
    [self.delegate.navigationController pushViewController:vc animated:NO];
    
    
}




- (void)viewDidLoad {
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    
   _isLand= [userDefaults boolForKey:ISLAND];
    
    [super viewDidLoad];
    _since=@0;
    
    self.view.backgroundColor=[UIColor whiteColor];
    isLoad=NO;
    [self netState];
    

}


- (void)netState{
    
    
    [self createUI];
    
    if (_isLand) {
        [self loadData];
        
    }

}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    
    [self loadData];
    
}




- (void)createUI{

    self.automaticallyAdjustsScrollViewInsets=NO;
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49) style:UITableViewStyleGrouped];;
    
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    [self.view addSubview:_tableView];
    
    _tableView.showsVerticalScrollIndicator=NO;
    WS(weakSelf)
    //添加下拉刷新
    [self.tableView addLegendHeaderWithRefreshingBlock:^{// 每次下拉都会产生效果  所以需要一次操作来停止效果
        if (!weakSelf.isLoad) {
            //开始数据请求
            _since=@0;
            isLoad=!isLoad;
            [weakSelf loadData];
        }
        else
        {
            [weakSelf.tableView.header endRefreshing];//如果正在运行 的时候拉一下会进入else但是 会产生效果   需要停止这个效果 每次下拉都会产生效果  所以需要一次操作来停止效果
            
        }
    }];
    
    
    
    //添加上拉加载
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        
        if (!weakSelf.isLoad)//如果没有加载为真 进入方法
        {
            _since=[NSNumber numberWithInteger:[_since integerValue]+20 ];
            isLoad=!isLoad;//加载状态
            [weakSelf loadData];//异步执行完完毕之后会变成未加载  否则再次下拉进入else方法
            
            //            JZJLog(@"_number%zd",_number);
            
        }else{//异步请求执行完毕之后isLoad才会变成NO 否则再次下拉会一直执行这个方法
            [weakSelf.tableView.footer endRefreshing];
            
        }
        
        
    }];
    


}
- (void)loadData
{
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    
    
    [self.tableView.header endRefreshing];//如果正在运行 的时候拉一下会进入else但是 会产生效果   需要停止这个效果 每次下拉都会产生效果  所以需要一次操作来停止效果

    [self.tableView.footer endRefreshing];

    
  
    [DownLoad getQuickLookFavItemWithsince:_since andBlock:^(NSDictionary* obj, NSError *error) {
        
        
        
        if ([_since isEqual:@0]) {
            //下拉刷新
            _dataArray=[NSMutableArray array];
            [self.tableView.header endRefreshing];
            
            
        }else{
            [self.tableView.footer endRefreshing];
        }
        
       


        if (!obj||![userdefault boolForKey:ISCONNECTED]) {
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
            isLoad=NO;
            return;
        }
        
    NSArray* commics=obj[@"data"][@"comics"];
      
        
        [commics enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
            
            Model* model=[[Model alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [_dataArray addObject:model];
            
            
        }];
        
    
        [self.tableView reloadData];
        isLoad=NO;//这是一个异步执行方法 当这个异步操作执行完毕之后 isLoad才会变成NO
        
        
    }];





}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    if (_isLand) {
        if (_dataArray.count) {
            return _dataArray.count;

        }else{
            
            return 1;
        }
       

    }else{
        
        
        return 1;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_isLand) {
        
        if (_dataArray.count) {
            
        
        QuickLookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuickLookCellLeft"];
        
        
        if (!cell) {
            
            cell=[[NSBundle mainBundle]loadNibNamed:@"QuickLookCellLeft" owner:nil options:nil][0];
            
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//无选中效果
            
            cell.TableViewControllerDelegate=self;
        }
        
        
        Model* model=_dataArray[indexPath.section];
        
        
        
        [cell refreshWithModel:model WithIndexPath:indexPath];
        return cell;
        }else
        {
            
            
            
            
            
            MainPageLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainPageLeftIslandCell"];
            
            
            if (!cell) {
                
                cell=[[NSBundle mainBundle]loadNibNamed:@"MainPageLeftIslandCell" owner:nil options:nil][0];
                
                cell.selectionStyle=UITableViewCellSelectionStyleNone;//无选中效果
        
            }
            
            
            return cell;

            
            
        }
        
        

        
    }else{
        
        
        MainPageLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainPageLeftCell"];
        
        
        if (!cell) {
            
            cell=[[NSBundle mainBundle]loadNibNamed:@"MainPageLeftCell" owner:nil options:nil][0];
            
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//无选中效果
            
        }
        
       
        return cell;

        
        
        
    }
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isLand&&_dataArray.count) {
       
            
            return 220;
    }
    
    
    return _tableView.frame.size.height;

    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (_isLand&&_dataArray.count) {
        
        
        return 35;
    }
    
    
    

   
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 1;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isLand) {
        
        if (_dataArray.count) {//如果请求的数据不为空
            
            Model* model=_dataArray[indexPath.section];//tableview是分组的 所以改用分组形势
            
            WebViewController * vc=[[WebViewController alloc]init];
            
            
            vc.model=model;
            
            vc.hidesBottomBarWhenPushed=YES;
            
            [self.delegate.navigationController pushViewController:vc animated:YES];
            

        }else{//如果请求的数据为空

            
            RightSearchController* vc=[[RightSearchController alloc]init];
            
            vc.hidesBottomBarWhenPushed=YES;
            
            [self.delegate.navigationController pushViewController:vc animated:YES];

            
                  }
       
    }else{//如果未登录 跳转到登录页面
        
        
        
        
        LandController* vc=[[LandController alloc]init];
        
        [self presentViewController:vc animated:YES completion:nil];
        

        
        
        
    }
    
    
    

  }



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UITableViewHeaderFooterView* view=(UITableViewHeaderFooterView*)[tableView dequeueReusableCellWithIdentifier:@"ID"];
    
    
        if (!view&&_dataArray.count) {
            
            
            view=[[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"ID"];
            view.backgroundColor=[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
            UILabel* lable=[[UILabel alloc]initWithFrame:CGRectMake(30, 7.5, 100, 20)];
            lable.textColor=JZJLIGHTGRAY;
            
            
            lable.tag=1000+section;
            lable.font= [UIFont boldSystemFontOfSize:15];
            [view.contentView addSubview:lable];
            
            UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 7.5, 20, 20)];
            
            imageView.image=[UIImage imageNamed:@"headerViewIcon.png"];
            
            [view.contentView addSubview:imageView];
            
            
            
            
        }
    
    if (_dataArray.count) {
        
        Model* model=_dataArray[section];
        NSString *str=[NSString stringWithFormat:@"%@",model.created_at];//时间戳
        NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        //    JZJLog(@"date:%@",[detaildate description]);
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"MM-dd"];
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        
        UILabel* lable=(UILabel*)[view viewWithTag:1000+section];
        
        lable.text=[NSString stringWithFormat:@"%@更新",currentDateStr];
        

    }
    
    
    return view;
}



@end
